package com.xingchen.loveparttimejob.uitls;

/**
 * @author 小志
 * @date 2024/10/12 - 10:34
 */

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.List;

/**
 * 公用分页泛型类工具
 * @param <T>
 */
public class PagerUtil<T> {

    private int indexPage;//当前页
    private int showRows;//显示多少行
    private int totalRows;//数据总行数
    private int totalPage;//总页数
    private String search;//搜索的数据
    private List<T> data;//当前需要显示的数据

    public int getIndexPage() {
        return indexPage;
    }

    public void setIndexPage(int indexPage) {
        if(this.getTotalPage() < indexPage && this.getTotalPage() != 0){
            this.indexPage = this.getTotalPage();
        }else if(indexPage <= 1){
            this.indexPage = 1;
        }else{
            this.indexPage = indexPage;
        }

    }

    public int getShowRows() {
        return this.showRows;
    }

    public void setShowRows(int showRows) {
        this.showRows = showRows;
        if(this.totalRows != 0 && this.showRows != 0){
            setTotalPage();
        }
    }

    /**
     * 获取总行数
     * @return 总行数
     */
    public int getTotalRows() {
        return this.totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
        if(this.totalRows != 0 && this.showRows != 0){
            setTotalPage();
        }
    }

    public int getTotalPage() {
        return this.totalPage;
    }

    public void setTotalPage() {
        this.totalPage = (int) Math.ceil((double) this.getTotalRows() / this.getShowRows());
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("{");
        sb.append("\"indexPage\" : \"").append(indexPage).append('\"');
        sb.append(",\"showRows\" : \"").append(showRows).append('\"');
        sb.append(",\"totalRows\" : \"").append(totalRows).append('\"');
        sb.append(",\"totalPage\" : \"").append(totalPage).append('\"');
        sb.append(",\"search\" : \"").append(search).append('\"');
        sb.append(",\"data\" :").append(data);
        sb.append('}');
        return sb.toString();
    }
}
